package edu.gatech.volt2.drdoommgt.presenter;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import javax.swing.JCheckBox;
import javax.swing.JOptionPane;

import edu.gatech.volt2.drdoommgt.domain.*;
import edu.gatech.volt2.drdoommgt.gui.*;
import edu.gatech.volt2.drdoommgt.system.DataBaseAccessor;


public class ViewAppointmentPresenter implements ActionListener{
	
	private static DataBaseAccessor dba;
	private Appointment myAppointment;
	private IViewAppointmentView myView;
	private int option;
	
	public ViewAppointmentPresenter(int option,Appointment myAppointment, IViewAppointmentView view){
		this.myAppointment= myAppointment;
		this.myView= view;
		myView.addListener(this);
		
		dba = new DataBaseAccessor();
		dba.connectToDatabase();
		List<Doctor> docList = dba.getAllDoctors();
		myView.setDoctorsList(docList);
		
		this.option= option;
		
		myView.showSelf();
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		if ( event.getSource() != myView.getCancelButton()){
			dba= new DataBaseAccessor();
			dba.connectToDatabase();
		}
		
		if ( event.getSource() == myView.getCancelButton()){
			myView.hideSelf();
		}
		
		else if(event.getSource() == myView.getSubmitButton()){
			if(option == 0)//view
			{
				this.viewAppointmentProcess();
				dba.closeConnectionToDatabase();
			}
			else if(option == 1)//edit
			{
				this.editAppointmentProcess();
			}
			else if(option == 2)//delete
			{
				this.deleteAppointmentProcess();
			}
			else//cancel
			{
				//do nothing.
			}

		}
		else{}//do nothing
	}
	
	
	
	public void viewAppointmentProcess(){
		@SuppressWarnings("deprecation")
		Calendar cal = Calendar.getInstance();
		int year = Integer.parseInt(myView.getWhenYear().getSelectedItem().toString());
		int month = Integer.parseInt(myView.getWhenMonth().getSelectedItem().toString());
		int date = Integer.parseInt(myView.getWhenDay().getSelectedItem().toString());
		System.out.println(month);
		cal.set(year, month, date);

		Doctor doc = dba.find(Doctor.class, ((Doctor)myView.getDoctorName().getSelectedItem()).getId());
		ArrayList<Appointment> appts= dba.getDoctorAppointments(doc);
		
		if(appts != null){
			if(myView.getChckbxNewCheckBox().isSelected()){
				Appointment[] apptArr= new Appointment[appts.size()];
				for(int i=0; i< apptArr.length; i++){
					apptArr[i]= appts.get(i);
				}
				
				String[] optionDisplay= new String[apptArr.length];
				for(int j=0; j<apptArr.length; j++){
					String when= apptArr[j].getRequestedDate().getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH) +
					" " + Integer.toString(apptArr[j].getRequestedDate().get(Calendar.DAY_OF_MONTH)) +
					", " + Integer.toString(apptArr[j].getRequestedDate().get(Calendar.YEAR)) +
					" - " + Integer.toString(apptArr[j].getRequestedDate().get(Calendar.HOUR)) +
					":" + (apptArr[j].getRequestedDate().get(Calendar.MINUTE) < 10 ? "0"+Integer.toString(apptArr[j].getRequestedDate().get(Calendar.MINUTE)):Integer.toString(apptArr[j].getRequestedDate().get(Calendar.MINUTE)) )+
					" " + apptArr[j].getRequestedDate().getDisplayName(Calendar.AM_PM, Calendar.SHORT, Locale.ENGLISH);
					optionDisplay[j]= when + " - " + apptArr[j].getPatientName();
				}
				
				String choice= (String) JOptionPane.showInputDialog(null,"Choose an appointment", "Dr. Doom Management", JOptionPane.YES_NO_CANCEL_OPTION, null, optionDisplay, optionDisplay[0]); //0-patient, 1-doctor, 2-nurse, 3-doctor, 4-cancel
				int choiceOption= getChosenIndex(optionDisplay, choice);
				System.out.println(choiceOption);
				
				JOptionPane.showMessageDialog(null,appts.get(choiceOption).toString());
				
			}
			else{// choose only from ones of indicated date
				ArrayList<Appointment> dayAppt= new ArrayList<Appointment>();
				for(int h=0; h<appts.size(); h++){
					int aYear= appts.get(h).getRequestedDate().get(Calendar.YEAR);
					int aDate= appts.get(h).getRequestedDate().get(Calendar.DAY_OF_MONTH);
					int aMonth= appts.get(h).getRequestedDate().get(Calendar.MONTH) + 1; //month + 1 b/c of the format of Calendar
					
					System.out.println(month + "/" + cal.get(Calendar.DAY_OF_MONTH) + "/" + cal.get(Calendar.YEAR));
					System.out.println(aMonth + "/" + aDate + "/" + aYear);

					if(month==aMonth && cal.get(Calendar.DAY_OF_MONTH)==aDate && year==aYear){
						dayAppt.add(appts.get(h));
						System.out.println(appts.get(h).getReason());
					}
				}//end for
				
				if(dayAppt.size()>0){
					
					Appointment[] dayApptArr= new Appointment[dayAppt.size()];
					for(int i=0; i< dayApptArr.length; i++){
						dayApptArr[i]= dayAppt.get(i);
						System.out.println(dayApptArr[i].getReason());
					}
					
					String[] optionDisplay= new String[dayApptArr.length];
					for(int j=0; j<dayApptArr.length; j++){
						String when= dayApptArr[j].getRequestedDate().getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH) +
						" " + Integer.toString(dayApptArr[j].getRequestedDate().get(Calendar.DAY_OF_MONTH)) +
						", " + Integer.toString(dayApptArr[j].getRequestedDate().get(Calendar.YEAR)) +
						" - " + Integer.toString(dayApptArr[j].getRequestedDate().get(Calendar.HOUR)) +
						":" + (dayApptArr[j].getRequestedDate().get(Calendar.MINUTE) < 10 ? "0"+Integer.toString(dayApptArr[j].getRequestedDate().get(Calendar.MINUTE)):Integer.toString(dayApptArr[j].getRequestedDate().get(Calendar.MINUTE)) )+
						" " + dayApptArr[j].getRequestedDate().getDisplayName(Calendar.AM_PM, Calendar.SHORT, Locale.ENGLISH);
						optionDisplay[j]= when + " - " + dayApptArr[j].getPatientName();
					}
					
					String dayChoice= (String) JOptionPane.showInputDialog(null,"Choose an appointment", "Dr. Doom Management", JOptionPane.YES_NO_CANCEL_OPTION, null, optionDisplay, optionDisplay[0]); //0-patient, 1-doctor, 2-nurse, 3-doctor, 4-cancel
					int dayOption= getChosenIndex(optionDisplay, dayChoice);
					
					JOptionPane.showMessageDialog(null, dayAppt.get(dayOption).toString());
					
				}
				else{
					System.out.println("No appointments on this date.");
				}
			}
		}
		else{
			JOptionPane.showMessageDialog(null, "No appointments.");
		}
	}
	
	
	public void editAppointmentProcess(){
		@SuppressWarnings("deprecation")
		Calendar cal = Calendar.getInstance();
		int year = Integer.parseInt(myView.getWhenYear().getSelectedItem().toString());
		int month = Integer.parseInt(myView.getWhenMonth().getSelectedItem().toString());
		int date = Integer.parseInt(myView.getWhenDay().getSelectedItem().toString());

		cal.set(year, month, date);

		Doctor doc = dba.find(Doctor.class, ((Doctor)myView.getDoctorName().getSelectedItem()).getId());
		ArrayList<Appointment> appts= dba.getDoctorAppointments(doc);
		
		if(appts != null){
			if(myView.getChckbxNewCheckBox().isSelected()){
				Appointment[] apptArr= new Appointment[appts.size()];
				for(int i=0; i< apptArr.length; i++){
					apptArr[i]= appts.get(i);
				}
				
				String[] optionDisplay= new String[apptArr.length];
				for(int j=0; j<apptArr.length; j++){
					String when= apptArr[j].getRequestedDate().getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH) +
					" " + Integer.toString(apptArr[j].getRequestedDate().get(Calendar.DAY_OF_MONTH)) +
					", " + Integer.toString(apptArr[j].getRequestedDate().get(Calendar.YEAR)) +
					" - " + Integer.toString(apptArr[j].getRequestedDate().get(Calendar.HOUR)) +
					":" + (apptArr[j].getRequestedDate().get(Calendar.MINUTE) < 10 ? "0"+Integer.toString(apptArr[j].getRequestedDate().get(Calendar.MINUTE)):Integer.toString(apptArr[j].getRequestedDate().get(Calendar.MINUTE)) )+
					" " + apptArr[j].getRequestedDate().getDisplayName(Calendar.AM_PM, Calendar.SHORT, Locale.ENGLISH);
					optionDisplay[j]= when + " - " + apptArr[j].getPatientName();
				}
				
				String choice= (String) JOptionPane.showInputDialog(null,"Choose an appointment", "Dr. Doom Management", JOptionPane.YES_NO_CANCEL_OPTION, null, optionDisplay, optionDisplay[0]); //0-patient, 1-doctor, 2-nurse, 3-doctor, 4-cancel
				int choiceOption= getChosenIndex(optionDisplay, choice);
				
				
				//JOptionPane.showMessageDialog(null,appts.get(choiceOption).toString());
				dba.save(appts.get(choiceOption));
				dba.closeConnectionToDatabase();
				EditAppointmentPresenter pres= new EditAppointmentPresenter(appts.get(choiceOption), new AppointmentRequestWindow(), appts.get(choiceOption).getPatientName());

				
			}
			else{// choose only from ones of indicated date
				ArrayList<Appointment> dayAppt= new ArrayList<Appointment>();
				for(int h=0; h<appts.size(); h++){
					int aYear= appts.get(h).getRequestedDate().get(Calendar.YEAR);
					int aDate= appts.get(h).getRequestedDate().get(Calendar.DAY_OF_MONTH);
					int aMonth= appts.get(h).getRequestedDate().get(Calendar.MONTH) + 1; //month + 1 b/c of the format of Calendar

					if(cal.get(Calendar.MONTH)==aMonth && cal.get(Calendar.DAY_OF_MONTH)==aDate && cal.get(Calendar.YEAR)==aYear){
						dayAppt.add(appts.get(h));
					}
				}//end for
				
				if(dayAppt.size()>0){
					
					Appointment[] dayApptArr= new Appointment[dayAppt.size()];
					for(int i=0; i< dayApptArr.length; i++){
						dayApptArr[i]= appts.get(i);
					}
					
					String[] optionDisplay= new String[dayApptArr.length];
					for(int j=0; j<dayApptArr.length; j++){
						String when= dayApptArr[j].getRequestedDate().getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH) +
						" " + Integer.toString(dayApptArr[j].getRequestedDate().get(Calendar.DAY_OF_MONTH)) +
						", " + Integer.toString(dayApptArr[j].getRequestedDate().get(Calendar.YEAR)) +
						" - " + Integer.toString(dayApptArr[j].getRequestedDate().get(Calendar.HOUR)) +
						":" + (dayApptArr[j].getRequestedDate().get(Calendar.MINUTE) < 10 ? "0"+Integer.toString(dayApptArr[j].getRequestedDate().get(Calendar.MINUTE)):Integer.toString(dayApptArr[j].getRequestedDate().get(Calendar.MINUTE)) )+
						" " + dayApptArr[j].getRequestedDate().getDisplayName(Calendar.AM_PM, Calendar.SHORT, Locale.ENGLISH);
						optionDisplay[j]= when + " - " + dayApptArr[j].getPatientName();
					}
					
					String dayChoice= (String) JOptionPane.showInputDialog(null,"Choose an appointment", "Dr. Doom Management", JOptionPane.YES_NO_CANCEL_OPTION, null, optionDisplay, optionDisplay[0]); //0-patient, 1-doctor, 2-nurse, 3-doctor, 4-cancel
					int dayOption= getChosenIndex(optionDisplay, dayChoice);
					
					//JOptionPane.showMessageDialog(null,appts.get(dayOption).toString());
					dba.save(appts.get(dayOption));
					dba.closeConnectionToDatabase();

					EditAppointmentPresenter pres= new EditAppointmentPresenter(appts.get(dayOption), new AppointmentRequestWindow(), appts.get(dayOption).getPatientName());
					
				}
				else{
					dba.closeConnectionToDatabase();

					System.out.println("No appointments on this date.");
				}
			}
		}
		else{
			dba.closeConnectionToDatabase();
			JOptionPane.showMessageDialog(null, "No appointments.");
		}
	}
	
	public void deleteAppointmentProcess(){
		@SuppressWarnings("deprecation")
		Calendar cal = Calendar.getInstance();
		int year = Integer.parseInt(myView.getWhenYear().getSelectedItem().toString());
		int month = Integer.parseInt(myView.getWhenMonth().getSelectedItem().toString());
		int date = Integer.parseInt(myView.getWhenDay().getSelectedItem().toString());
		System.out.println(month);
		cal.set(year, month, date);

		Doctor doc = dba.find(Doctor.class, ((Doctor)myView.getDoctorName().getSelectedItem()).getId());
		ArrayList<Appointment> appts= dba.getDoctorAppointments(doc);
		
		if(appts != null){
			if(myView.getChckbxNewCheckBox().isSelected()){
				Appointment[] apptArr= new Appointment[appts.size()];
				for(int i=0; i< apptArr.length; i++){
					apptArr[i]= appts.get(i);
				}
				
				String[] optionDisplay= new String[apptArr.length];
				for(int j=0; j<apptArr.length; j++){
					String when= apptArr[j].getRequestedDate().getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH) +
					" " + Integer.toString(apptArr[j].getRequestedDate().get(Calendar.DAY_OF_MONTH)) +
					", " + Integer.toString(apptArr[j].getRequestedDate().get(Calendar.YEAR)) +
					" - " + Integer.toString(apptArr[j].getRequestedDate().get(Calendar.HOUR)) +
					":" + (apptArr[j].getRequestedDate().get(Calendar.MINUTE) < 10 ? "0"+Integer.toString(apptArr[j].getRequestedDate().get(Calendar.MINUTE)):Integer.toString(apptArr[j].getRequestedDate().get(Calendar.MINUTE)) )+
					" " + apptArr[j].getRequestedDate().getDisplayName(Calendar.AM_PM, Calendar.SHORT, Locale.ENGLISH);
					optionDisplay[j]= when + " - " + apptArr[j].getPatientName();
				}
				
				String choice= (String) JOptionPane.showInputDialog(null,"Choose an appointment", "Dr. Doom Management", JOptionPane.YES_NO_CANCEL_OPTION, null, optionDisplay, optionDisplay[0]); //0-patient, 1-doctor, 2-nurse, 3-doctor, 4-cancel
				int choiceOption= getChosenIndex(optionDisplay, choice);
				System.out.println(choiceOption);
				
				dba.removeAppointment(appts.get(choiceOption));
			}
			else{// choose only from ones of indicated date
				ArrayList<Appointment> dayAppt= new ArrayList<Appointment>();
				for(int h=0; h<appts.size(); h++){
					int aYear= appts.get(h).getRequestedDate().get(Calendar.YEAR);
					int aDate= appts.get(h).getRequestedDate().get(Calendar.DAY_OF_MONTH);
					int aMonth= appts.get(h).getRequestedDate().get(Calendar.MONTH) + 1; //month + 1 b/c of the format of Calendar
					
					System.out.println(month + "/" + cal.get(Calendar.DAY_OF_MONTH) + "/" + cal.get(Calendar.YEAR));
					System.out.println(aMonth + "/" + aDate + "/" + aYear);

					if(month==aMonth && cal.get(Calendar.DAY_OF_MONTH)==aDate && year==aYear){
						dayAppt.add(appts.get(h));
						System.out.println(appts.get(h).getReason());
					}
				}//end for
				
				if(dayAppt.size()>0){
					
					Appointment[] dayApptArr= new Appointment[dayAppt.size()];
					for(int i=0; i< dayApptArr.length; i++){
						dayApptArr[i]= dayAppt.get(i);
						System.out.println(dayApptArr[i].getReason());
					}
					
					String[] optionDisplay= new String[dayApptArr.length];
					for(int j=0; j<dayApptArr.length; j++){
						String when= dayApptArr[j].getRequestedDate().getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH) +
						" " + Integer.toString(dayApptArr[j].getRequestedDate().get(Calendar.DAY_OF_MONTH)) +
						", " + Integer.toString(dayApptArr[j].getRequestedDate().get(Calendar.YEAR)) +
						" - " + Integer.toString(dayApptArr[j].getRequestedDate().get(Calendar.HOUR)) +
						":" + (dayApptArr[j].getRequestedDate().get(Calendar.MINUTE) < 10 ? "0"+Integer.toString(dayApptArr[j].getRequestedDate().get(Calendar.MINUTE)):Integer.toString(dayApptArr[j].getRequestedDate().get(Calendar.MINUTE)) )+
						" " + dayApptArr[j].getRequestedDate().getDisplayName(Calendar.AM_PM, Calendar.SHORT, Locale.ENGLISH);
						optionDisplay[j]= when + " - " + dayApptArr[j].getPatientName();
					}
					
					String dayChoice= (String) JOptionPane.showInputDialog(null,"Choose an appointment", "Dr. Doom Management", JOptionPane.YES_NO_CANCEL_OPTION, null, optionDisplay, optionDisplay[0]); //0-patient, 1-doctor, 2-nurse, 3-doctor, 4-cancel
					int dayOption= getChosenIndex(optionDisplay, dayChoice);
					
					dba.removeAppointment(dayAppt.get(dayOption));
				}
				else{
					System.out.println("No appointments on this date.");
				}
			}
		}
		else{
			JOptionPane.showMessageDialog(null, "No appointments.");
		}
	}

	
	
	private int getChosenIndex(String[] arr, String target){
		int i=0;
		boolean found= false;
		
		while(!found){
			System.out.println(arr[i]);
			System.out.println(target);
			if(arr[i].equals(target)){
				found= true;
			}
			else{
				i++;
			}
		}
		return i;
	}
	
	
}
